package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjGoodsFieldContent;
import com.zb.entity.ZjGoodsParamFieldContent;
import com.zb.mapper.ZjGoodsParamFieldContentMapper;
import com.zb.service.ZjGoodsParamFieldContentService;
import com.zb.vo.GoodsFieldContentVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品参数字段内容表，当category=2/3时，内容存入该表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Service
public class ZjGoodsParamFieldContentServiceImpl extends ServiceImpl<ZjGoodsParamFieldContentMapper, ZjGoodsParamFieldContent> implements ZjGoodsParamFieldContentService {
    @Resource
    private ZjGoodsParamFieldContentMapper goodsParamFieldContentMapper;

    // 根据商品字段ID集合删除字段内容
    @Override
    public void deleteByParamFieldIds(List<Long> paramFieldIdList) {
        if (null == paramFieldIdList || paramFieldIdList.isEmpty()) {
            return;
        }
        goodsParamFieldContentMapper.deleteByParamFieldIds(paramFieldIdList);
    }

    // 新增或修改参数字段
    @Override
    @Transactional
    public void save(Long custAccId, Long paramFieldId, List<GoodsFieldContentVo> contentVos) {
        if (null == contentVos || contentVos.isEmpty()) {
            return;
        }
        Assert.isTrue(contentVos.stream().map(GoodsFieldContentVo::getVal).toList().size() == contentVos.stream().map(GoodsFieldContentVo::getVal).collect(Collectors.toSet()).size(),
                "参数字段内容已重复！", null);

        List<ZjGoodsParamFieldContent> adds = new ArrayList<>();
        List<ZjGoodsParamFieldContent> updates = new ArrayList<>();
        ZjGoodsParamFieldContent content = null;
        for (GoodsFieldContentVo vo : contentVos) {
            content = new ZjGoodsParamFieldContent();
            if (null != vo.getId()) {
                content.setId(vo.getId());
                content.setVal(vo.getVal());
                updates.add(content);
                continue;
            }
            content.setId(IdWorker.getId());
            content.setCustAccId(custAccId);
            content.setParamFieldId(paramFieldId);
            content.setVal(vo.getVal());
            adds.add(content);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据参数字段ID集合获取参数字段内容
    @Override
    public List<ZjGoodsParamFieldContent> getByParamFieldIds(List<Long> paramFieldIdList) {
        if (null == paramFieldIdList || paramFieldIdList.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjGoodsParamFieldContent> wrapper = new QueryWrapper<>();
        wrapper.in("param_field_id", paramFieldIdList);
        return this.list(wrapper);
    }

    @Override
    public List<ZjGoodsParamFieldContent> getByParamFieldId(Long paramFieldId) {
        if (null == paramFieldId) {
            return new ArrayList<>();
        }
        List<Long> paramFieldIdList = new ArrayList<>();
        paramFieldIdList.add(paramFieldId);
        return this.getByParamFieldIds(paramFieldIdList);
    }
}
